package com.linkface.utils;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;

/**
 * Copyright (c) 2017-2018 LINKFACE Corporation. All rights reserved.
 */

public class LFLightManager {
    private final Sensor mDefaultSensor;
    private final SensorManager mSystemService;

    private LFLightSensorListener mLightSensorListener;

    //光线传感器

    public LFLightManager(Context context) {
        mSystemService = (SensorManager) context.getSystemService(Context.SENSOR_SERVICE);
        mDefaultSensor = mSystemService.getDefaultSensor(Sensor.TYPE_LIGHT);

        mSystemService.registerListener(mSensorEventListener, mDefaultSensor, SensorManager.SENSOR_DELAY_UI);
    }

    public void destroy() {
        if (mSystemService != null) {
            mSystemService.unregisterListener(mSensorEventListener);
        }
    }

    public void registerLightSensorListener(LFLightSensorListener listener) {
        mLightSensorListener = listener;
    }

    private SensorEventListener mSensorEventListener = new SensorEventListener() {
        @Override
        public void onSensorChanged(SensorEvent event) {
            if (mLightSensorListener != null) {
                float value = event.values[0];
                mLightSensorListener.onLightChange(value);
            }
        }

        @Override
        public void onAccuracyChanged(Sensor sensor, int accuracy) {

        }
    };

    public interface LFLightSensorListener {
        void onLightChange(float light);
    }
}
